using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.UserDomain;
using ShivamEMR.Model.DataInterfaces.UsersDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;
using PagedList;

namespace ShivamEMR.Web.Areas.User.Controllers
{   
    public class UserRightController : Controller
    {
		private readonly IUserRightTypeRepository userrighttypeRepository;
		private readonly IUserRightGroupRepository userrightgroupRepository;
		private readonly IUserRightRepository userrightRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public UserRightController() 
        {
            userrightgroupRepository = Registry.RepositoryFactory.GetUserRightGroupRepository();
            userrightRepository = Registry.RepositoryFactory.GetUserRightRepository();
            userrighttypeRepository = Registry.RepositoryFactory.GetUserRightTypeRepository();
        }

        public UserRightController(IUserRightTypeRepository userrighttypeRepository, IUserRightGroupRepository userrightgroupRepository, IUserRightRepository userrightRepository)
        {
			this.userrighttypeRepository = userrighttypeRepository;
			this.userrightgroupRepository = userrightgroupRepository;
			this.userrightRepository = userrightRepository;
        }

        //
        // GET: /UserRight/
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index(string searchString, int? page)
        {
            var userrights = userrightRepository.All;
            if (!String.IsNullOrEmpty(searchString))
            {
                userrights = userrights.Where(ru => ru.Title.ToUpper().Contains(searchString.ToUpper())
                                   || ru.Type.Type.ToUpper().Contains(searchString.ToUpper())
                                   || ru.Group.Group.ToUpper().Contains(searchString.ToUpper())
                     );
            }
            var plist = userrights.ToList<ShivamEMR.Model.Domain.UserDomain.UserRight>();
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(plist.ToPagedList(pageNumber, pageSize));         
            //return View(userrightRepository.All);//Including(userright => userright.Type, userright => userright.Group, userright => userright.Permissions, userright => userright.UserRoles));
        }

        //
        // GET: /UserRight/Details/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(int id)
        {
            return View(userrightRepository.Find(id));
        }

        //
        // GET: /UserRight/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Create()
        {
			ViewBag.PossibleTypes = userrighttypeRepository.All;
			ViewBag.PossibleGroups = userrightgroupRepository.All;
            return View();
        } 

        //
        // POST: /UserRight/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(UserRight userright)
        {
            if (ModelState.IsValid) {
                userrightRepository.Create(userright);
                userrightRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleTypes = userrighttypeRepository.All;
				ViewBag.PossibleGroups = userrightgroupRepository.All;
				return View();
			}
        }
        
        //
        // GET: /UserRight/Edit/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleTypes = userrighttypeRepository.All;
			ViewBag.PossibleGroups = userrightgroupRepository.All;
             return View(userrightRepository.Find(id));
        }

        //
        // POST: /UserRight/Edit/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(UserRight userright)
        {
            if (ModelState.IsValid) {
                userrightRepository.Update(userright);
                userrightRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleTypes = userrighttypeRepository.All;
				ViewBag.PossibleGroups = userrightgroupRepository.All;
				return View();
			}
        }

        //
        // GET: /UserRight/Delete/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(int id)
        {
            return View(userrightRepository.Find(id));
        }

        //
        // POST: /UserRight/Delete/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var ur = userrightRepository.Find(id);
            userrightRepository.Delete(ur);
            userrightRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

